﻿
using Cognex.VisionPro.Dimensioning;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestProject1
{
    public class MyClass
    {
        public void ListTest()
        {
            List<string> list = new List<string>();
            list.Add("1");
            list.Add("2");
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }

        }
        public void DictionaryTest()
        {
            try
            {
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
                keyValuePairs.Add("1", "2");
                keyValuePairs.Add("1", "3");
                keyValuePairs.TryAdd("2", "4");
                foreach (KeyValuePair<string, string> item in keyValuePairs)
                {
                    Console.WriteLine(item.Key + " " + item.Value);

                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e.GetType());
            }
        }

        public void SerialPortTest()
        {
            SerialPort mySerialPort = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
            mySerialPort.Open();  // 打开串口
            //定义modbus-rtu发送报文
            byte[] sendBytes = new byte[8];
            sendBytes[0] = 0x01;
            sendBytes[1] = 0x03;
            sendBytes[2] = 100 / 256;
            sendBytes[3] = 100 % 256;
            sendBytes[4] = 0x00;
            sendBytes[5] = 0x01;
            GetCRC(sendBytes);
            mySerialPort.Write(sendBytes, 0, sendBytes.Length);
            //获取数据
            Thread.Sleep(50);
            byte[] receiveBytes = new byte[mySerialPort.BytesToRead];
            mySerialPort.Read(receiveBytes, 0, receiveBytes.Length);
            foreach (byte b in receiveBytes)
            {
                Console.WriteLine(b);
            }
        }


        private void GetCRC(byte[] arr)
        {
            ushort CRCReg = 0xFFFF;//定义crc寄存器
            for (int i = 0; i < arr.Length - 2; i++)
            {
                CRCReg ^= arr[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((CRCReg & 1) == 0)
                    {
                        CRCReg >>= 1;
                    }
                    else
                    {
                        CRCReg >>= 1;
                        CRCReg ^= 0xA001;
                    }
                }
            }
            arr[arr.Length - 2] = (byte)(CRCReg & 0x00FF);//低字节在前
            arr[arr.Length - 1] = (byte)(CRCReg >> 8);//高字节在后
        }
        //异或运算规则：
        //0^0=0,
        //1^1=0,
        //0^1=1
        //1^0=1
        public void INITest()
        {
            // 声明INI文件的写操作函数 WritePrivateProfileString()
            [System.Runtime.InteropServices.DllImport("kernel32")]
            static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

            // 声明INI文件的读操作函数 GetPrivateProfileString()
            [System.Runtime.InteropServices.DllImport("kernel32")]
            static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
            string path3 = Directory.GetCurrentDirectory() + "\\" + "test.ini";
            Console.WriteLine(path3);
            if (!File.Exists(path3))
            {
                File.Create(path3);
            }
            WritePrivateProfileString("基本信息", "IP", "172.1.1.1", path3);
            StringBuilder retVal = new StringBuilder();
            int a = GetPrivateProfileString("基本信息", "name", "", retVal, 13, path3);
            Console.WriteLine(retVal.ToString() + a);
        }

        public void FileTest()
        {
            string s1 = "测试";
            byte[] b = Encoding.Default.GetBytes(s1);
            foreach (var item in b)
            {
                Console.WriteLine(item);
            }
        }

        /*public void MySQLTest()
        {
            OperateClass operateClass = new OperateClass("127.0.0.1", "zsa01", "root", "zsa010619");
            Console.WriteLine(operateClass.select());
        }*/


        public void TestAtan2()
        {
            //double r=Math.Atan2(-1,-1);
            double a = Math.PI / 180 * 0.2;
            Console.WriteLine("   " + a);
        }

        public void TestCogCreateSegmentTool(double rad)
        {
            double X = (100 - 0) * Math.Cos(rad) - (200 - 0) * Math.Sin(rad) + 0;
            double Y = (100 - 0) * Math.Sin(rad) + (200 - 0) * Math.Cos(rad) + 0;
            Console.WriteLine(X + "    " + Y);

        }

        public static void FindCircleCenter(double x1, double y1, double x2, double y2, double theta)
        {
            // 将角度转换为弧度
            double thetaRad = theta * Math.PI / 180.0;

            // 计算中点
            double midX = (x1 + x2) / 2;
            double midY = (y1 + y2) / 2;

            // 计算旋转角的正弦和余弦
            double sinTheta = Math.Sin(thetaRad);
            double cosTheta = Math.Cos(thetaRad);

            // 计算圆心坐标
            double a = midX + (y1 - y2) * sinTheta / (2 * (1 - cosTheta));
            double b = midY + (x2 - x1) * sinTheta / (2 * (1 - cosTheta));

            Console.WriteLine($"{a},  {b}");
        }
        //public delegate void food(double angle);
        public event Action<double> Eat;
        //Action<double> action;
        public void EventTest() {
            Eat += TestCogCreateSegmentTool;
            if (Eat != null) {
                //action.Invoke(1);
                Eat(1);
            }
            //Eat += TestAtan2;

        }
    }
}
